package com.tourlaid.mamagerment.config

import com.tourlaid.mamagerment.enums.TldExceptionEnum
import com.tourlaid.mamagerment.util.LoginUtil
import com.tourlaid.mamagerment.util.TldException
import org.springframework.context.annotation.Configuration
import org.springframework.util.StringUtils
import org.springframework.web.servlet.config.annotation.InterceptorRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
 * Created by xiaoyongge on 2017/7/26.
 */
@Configuration
class WebAppConfig : WebMvcConfigurerAdapter() {

    override fun addInterceptors(registry: InterceptorRegistry?) {
        registry!!.addInterceptor(object : HandlerInterceptorAdapter() {

            override fun preHandle(request: HttpServletRequest, response: HttpServletResponse,
                                   handler: Any): Boolean {
                var userId: String? = request.getParameter("userId")
                if (StringUtils.isEmpty(userId)) {
                    throw TldException(TldExceptionEnum.UN_LOGIN)
                }
                LoginUtil.userId = userId!!.toLong()
                return true;
            }
        }
        ).addPathPatterns("/tld/admin/**");
    }

}